//
//  OrderQuantityCell.m
//  Ufan
//
//  Created by yangqingluo on 15/12/11.
//  Copyright © 2015年 yangqingluo. All rights reserved.
//

#import "OrderQuantityCell.h"
#import "UIResponder+Router.h"

@interface OrderQuantityCell()

@property (strong, nonatomic) UIView *bottomLineView1;
@property (strong, nonatomic) UIView *quantityView;
@property (strong, nonatomic) UILabel *quantityLabel;
@property (strong, nonatomic) UIButton *minusButton;
@property (strong, nonatomic) UIButton *plusButton;

@end

@implementation OrderQuantityCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.textLabel.font = [UIFont systemFontOfSize:14.0];
        
        self.detailTextLabel.font = self.textLabel.font;
        self.detailTextLabel.textColor = self.textLabel.textColor;
        
        _bottomLineView1 = [[UIView alloc] init];
        _bottomLineView1.backgroundColor = UFCOLOR_GRAYLINE;
        [self.contentView addSubview:_bottomLineView1];
        
        [self.contentView addSubview:self.quantityView];
    }
    return self;
}


-(void)layoutSubviews{
    [super layoutSubviews];
    _bottomLineView1.frame = CGRectMake(15, self.height - 1, self.width - 30, 1);
    
    self.textLabel.text = @"数量";
    
    self.quantityView.center = CGPointMake(_bottomLineView1.right - 0.5 * self.quantityView.width, self.textLabel.center.y);
    self.quantityLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)self.product.quantity];
}

- (void)reloadButtonState{
    self.minusButton.selected = self.product.quantity <= 1;
    self.plusButton.selected = self.product.quantity >= self.inventory;
    self.quantityLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)self.product.quantity];
    
    [self routerEventWithName:UF_Router_Event_ProductQuantity userInfo:nil];
}

- (void)minusClick:(UIButton *)button{
    if (!button.selected) {
        self.product.quantity--;
        [self reloadButtonState];
    }
}

- (void)plusClick:(UIButton *)button{
    if (!button.selected) {
        self.product.quantity++;
        [self reloadButtonState];
    }
}

+(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

#pragma getter
- (UIView *)quantityView{
    if (!_quantityView) {
        _quantityView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90, 30)];
        
        self.minusButton.center = CGPointMake(0.5 * self.minusButton.width, 0.5 * _quantityView.height);
        [_quantityView addSubview:self.minusButton];
        
        self.plusButton.center = CGPointMake(_quantityView.width - 0.5 * self.plusButton.width, 0.5 * _quantityView.height);
        [_quantityView addSubview:self.plusButton];
        
        self.quantityLabel.center = CGPointMake(0.5 * _quantityView.width, 0.5 * _quantityView.height);
        [_quantityView addSubview:self.quantityLabel];
    }
    
    return _quantityView;
}

- (UILabel *)quantityLabel{
    if (!_quantityLabel) {
        _quantityLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 30)];
        _quantityLabel.font = [UIFont systemFontOfSize:14.0];
        _quantityLabel.textColor = self.textLabel.textColor;
        _quantityLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _quantityLabel;
}

- (UIButton *)minusButton{
    if (!_minusButton) {
        _minusButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _minusButton.frame = CGRectMake(0, 0, 25, 25);
        _minusButton.layer.borderColor = UFCOLOR_GRAYLINE.CGColor;
        _minusButton.layer.borderWidth = 1;
        [_minusButton setTitle:@"﹣" forState:UIControlStateNormal];
        [_minusButton setTitleColor:[UIColor blackColor]  forState:UIControlStateNormal];
        [_minusButton setTitleColor:UFCOLOR_GRAYLINE  forState:UIControlStateSelected];
        [_minusButton addTarget:self action:@selector(minusClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _minusButton;
}

- (UIButton *)plusButton{
    if (!_plusButton) {
        _plusButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _plusButton.frame = CGRectMake(0, 0, 25, 25);
        _plusButton.layer.borderColor = UFCOLOR_GRAYLINE.CGColor;
        _plusButton.layer.borderWidth = 1;
        [_plusButton setTitle:@"﹢" forState:UIControlStateNormal];
        [_plusButton setTitleColor:[UIColor blackColor]  forState:UIControlStateNormal];
        [_plusButton setTitleColor:UFCOLOR_GRAYLINE  forState:UIControlStateSelected];
        [_plusButton addTarget:self action:@selector(plusClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _plusButton;
}
    
@end
